package com.johnburrin.mortgage.estimator;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

public class MortgageCalculatorActivity extends Activity {
	ImageButton btnDoCalc = null;
	ImageButton btnPrevious = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mortage_calculator);
		btnDoCalc = (ImageButton) findViewById(R.id.imageButtonCalculate);
		btnDoCalc.setOnClickListener(onClickHandler);
		btnPrevious = (ImageButton) findViewById(R.id.imageButtonPrevious);
		btnPrevious.setOnClickListener(onClickHandler);

	}

	View.OnClickListener onClickHandler = new View.OnClickListener() {
		public void onClick(View v) {
			// TODO Use this for all click events
			switch (v.getId()){
			case R.id.imageButtonCalculate : doCalc();break;
			case R.id.imageButtonPrevious : finish(); break;
			}
		}

	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}


	private String doCalc(){
		String total2 = null;
		try {
			final EditText amount = (EditText)findViewById(R.id.MortgageAmount);
			final EditText interestPerMonth = (EditText)findViewById(R.id.InterestRate);
			final EditText loanLength = (EditText)findViewById(R.id.RepaymentTerm);
			final double a = Double.parseDouble(amount.getText().toString()); 
			final double i = Double.parseDouble(interestPerMonth.getText().toString());
			final double l = Double.parseDouble(loanLength.getText().toString());
			final double x = financial.pmt(a, i, l);
			final EditText repayments = (EditText)findViewById(R.id.editText1);

			
			total2 = String.format("%.2f", x);

			repayments.setText(total2);

		} catch (Exception e){
			//  throw new RuntimeException(e);
		}
		return total2;


	}
}
